package com.jicheng.service.impl;


import com.jicheng.dto.app.*;
import com.jicheng.entity.AppManager;
import com.jicheng.ext.dto.PagingResult;
import com.jicheng.ext.util.BeanConvertor;
import com.jicheng.ext.util.UUIDUtils;
import com.jicheng.mapper.AppManagerMapper;
import com.jicheng.service.IAppService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppServiceImpl implements IAppService {

    @Autowired
    private AppManagerMapper appManagerMapper;


    @Transactional
    @Override
    public void saveVersion(AppParams params) {
        notNull("params",params);
        notNull("version",params.getVersion());
        notNull("path",params.getPath());

        AppManager appManager = BeanConvertor.copy(params, AppManager.class);
        if (StringUtils.isEmpty(appManager.getId())) {
            appManager.setId(UUIDUtils.uuid());
            appManagerMapper.insertSelective(appManager);
        } else {
            appManagerMapper.updateByPrimaryKeySelective(appManager);
        }
    }

    @Override
    public PagingResult<AppDto> getList(AppPagingParams params) {
        notNull("params",params);
        PagingResult<AppDto> result = new PagingResult();
        List<AppDto> list = appManagerMapper.getList(params);
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        result.setList(list);
        result.setTotal(appManagerMapper.getCount(params));
        return result;
    }

    @Override
    public Map getLastVersion() {
        return appManagerMapper.getLastVersion();
    }

    @Override
    public AppDto getById(String id) {
        notNull("id", id);
        AppManager appManager = appManagerMapper.selectByPrimaryKey(id);
        return appManager ==null?null:BeanConvertor.copy(appManager,AppDto.class);
    }

    @Override
    public void saveAppHistory(AppHistoryParams appHistoryParams) {
        appHistoryParams.setId(UUIDUtils.uuid());
//        appHistoryParams.setUpdateTime(new Date());
       appManagerMapper.saveAppHistory(appHistoryParams);
    }

    @Override
    public List<AppUpdateDto> getAppUpdateList(AppPagingParams appPagingParams) {
        List<AppUpdateDto> list = appManagerMapper.getAppUpdateList(appPagingParams);
        return list;
    }

    @Override
    public List<AppHistoryDto> getAppHistoryList(String appId,String startTime,String endTime) {
        List<AppHistoryDto> list = appManagerMapper.getAppHistoryList(appId,startTime,endTime);
        return list;
    }

    @Override
    public void addMonitoringList(MonitoringList params) {
        appManagerMapper.addMonitoringList(params);
    }

    @Override
    public List<MonitoringList> getMonitoringList(MonitoringPagingParams params) {

        return appManagerMapper.getMonitoringList(params);
    }

    @Override
    public void updateMonitoringList(MonitoringList params) {
        appManagerMapper.updateMonitoringList(params);
    }

    @Override
    public void delMonitoringList(String id) {
        appManagerMapper.delMonitoringList(id);
    }

    @Override
    public MonitoringList checkMonitoringList(String id) {
        return appManagerMapper.checkMonitoringList(id);
    }

    @Override
    public Map<String,List<String>> getConditions() {
        Map<String,List<String>> map = new HashMap<>();
        //获取所属业务
       List<String> business  = appManagerMapper.getBusiness();
      // 获取监督类型
        List<String> type  = appManagerMapper.getType();
        map.put("business",business);
        map.put("type",type);
        return map;
    }

}
